//
//  MainCollectionViewController.m
//  FoodBible
//
//  Created by lanou3g on 15/7/17.
//  Copyright (c) 2015年 NB三人行. All rights reserved.
//

#import "MainCollectionViewController.h"
#import "MainFkowLayout.h"
#import "MainCollectionViewCell.h"
#import "IntroControll.h"
#import "AllClassifyTableViewController.h"
#import "RightTableViewController.h"
#import "DetailTableViewController.h"
#import "ImageDownLoader.h"
#import "IntroModel.h"
#import "UIImageView+WebCache.h"

// Strings
NSString * const kTWMessageViewControllerCellIdentifier = @"kTWMessageViewControllerCellIdentifier";

// Numerics
CGFloat const kTWMessageViewControllerCellPadding = 10;
CGFloat const kTWMessageViewControllerCellHeight = 50;

@interface MainCollectionViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic,strong) NSMutableArray *urlArray;
@property (nonatomic,strong) NSMutableArray *burdenArray;
@property (nonatomic,strong) NSMutableArray *titleArray;
@property (nonatomic,strong) NSMutableArray *nameArray;
@property (nonatomic,strong) NSMutableArray *ingredientsArray;
@property (nonatomic,strong) NSMutableArray *stepsArray;
@property (nonatomic,strong) NSDictionary *stepsDic;


@end

@implementation MainCollectionViewController

#pragma mark - Alloc/Init

- (id)init
{
    self = [super initWithCollectionViewLayout: [[MainFkowLayout alloc] init]];
    self = [super initWithNibName:nil bundle:nil];
    //    self.wantsFullScreenLayout = YES;
    self.modalPresentationStyle = UIModalPresentationFullScreen;
    
    if (self)
    {
        
    }
    return self;
}

#pragma mark - View Lifecycle

- (void)loadView
{
    [super loadView];
    
    self.navigationItem.title = @"美食宝典";
    [self mainLayout];
    [self rightButtonAndLeftButton];
    [self.collectionView registerClass:[MainCollectionViewCell class] forCellWithReuseIdentifier:kTWMessageViewControllerCellIdentifier];
//    RequestData *request = [[RequestData alloc] init];
//    [request requestWithUrl:@"http://apis.juhe.cn/cook/index?key=a8be7fa328b601bbb1e55ef1084f9bbb&cid=57"];
////    request.delegate = self;
    
    
    NSURL *url = [NSURL URLWithString:@"http://apis.juhe.cn/cook/index?key=a8be7fa328b601bbb1e55ef1084f9bbb&cid=57"];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    NSURLResponse *respone = nil;
    
    NSError  *error = nil;
    
    NSData *data =  [NSURLConnection sendSynchronousRequest:request returningResponse:&respone error:&error];
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSDictionary *resultDic = [dic objectForKey:@"result"];
    
    NSArray *dataArray = [resultDic objectForKey:@"data"];
    
    
    self.urlArray = [NSMutableArray array];
    self.stepsArray = [NSMutableArray array];
    self.burdenArray = [NSMutableArray array];
    self.ingredientsArray = [NSMutableArray array];
    self.titleArray = [NSMutableArray array];
    self.stepsDic = [NSDictionary dictionary];
    
    for (NSDictionary *dic in dataArray) {
        
        NSArray *array = [dic objectForKey:@"albums"];
        NSString *urlString = array[0];
        [self.urlArray addObject:urlString];
        
        NSString *burdenString = [dic objectForKey:@"burden"];
        [self.burdenArray addObject:burdenString];
        
        NSString *title = [dic objectForKey:@"title"];
        [self.titleArray addObject:title];
        
        self.stepsDic = [dic objectForKey:@"steps"];
        
        NSString *ingredientsStr = [dic objectForKey:@"ingredients"];
        [self.ingredientsArray addObject:ingredientsStr];
        
        
    }
    
}


-(void)mainLayout{
    // Background
    UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"messages_bg_2.png"]];
    backgroundImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    backgroundImageView.frame=self.view.frame;
    [self.view insertSubview:backgroundImageView belowSubview:self.collectionView];
    
    // Parallax effect
    UIInterpolatingMotionEffect *interpolationHorizontal = [[UIInterpolatingMotionEffect alloc]initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
    interpolationHorizontal.minimumRelativeValue = @-20.0;
    interpolationHorizontal.maximumRelativeValue = @20.0;
    
    UIInterpolatingMotionEffect *interpolationVertical = [[UIInterpolatingMotionEffect alloc]initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
    interpolationVertical.minimumRelativeValue = @-20.0;
    interpolationVertical.maximumRelativeValue = @20.0;
    
    [backgroundImageView addMotionEffect:interpolationHorizontal];
    [backgroundImageView addMotionEffect:interpolationVertical];
    
    // Configurte collection view
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.collectionView registerClass:[MainCollectionViewCell class] forCellWithReuseIdentifier:kTWMessageViewControllerCellIdentifier];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
}

-(void) rightButtonAndLeftButton{
    
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-class.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftItemClick)];
    
    self.navigationItem.leftBarButtonItem = leftButton;
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"user.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightItemClick)];
    self.navigationItem.rightBarButtonItem = rightButton;
}

-(void)leftItemClick{
    AllClassifyTableViewController *allClassify = [[AllClassifyTableViewController alloc] init];
    
    //动画
    CATransition *transition = [CATransition animation];
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionReveal;
    transition.startProgress = 0.5;
    transition.delegate = self;
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    self.navigationController.navigationBarHidden = NO;
    
    [self.navigationController pushViewController:allClassify animated:YES];
}

-(void)rightItemClick{
    RightTableViewController *right = [[RightTableViewController alloc] init];
    [self.navigationController pushViewController:right animated:YES];

}


#pragma mark - UICollectionView DataSource & Delegate methods

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.titleArray.count;
}

-(BOOL)collectionView:(UICollectionView *)collectionView shouldDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    DetailTableViewController *detailVC = [[DetailTableViewController alloc] init];
    
    detailVC.title = self.titleArray[indexPath.row];
    detailVC.ingredients = self.ingredientsArray[indexPath.row];
    detailVC.burden = self.burdenArray[indexPath.row];
    detailVC.stepsArray = self.stepsArray[indexPath.row];
    
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MainCollectionViewCell *cell = (MainCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:kTWMessageViewControllerCellIdentifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.5];
    
    cell.titleLable.text = self.titleArray[indexPath.row];
//    cell.image.image = [UIImage imageNamed:@"foodDetail_background.jpg"];

    [cell.image sd_setImageWithURL:self.urlArray[indexPath.row]];
    
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(self.view.bounds.size.width - (kTWMessageViewControllerCellPadding*2), kTWMessageViewControllerCellHeight*3);
}

@end
